package ticker

import (
	"time"
	"gini2p/models"
	"gini2p/logger"
	"fmt"
	"strconv"
)

func RecongnizeRate() {
	tick := time.NewTicker(time.Minute * 5)

	now := time.Now().Format("2006-01-02")
	t, _ := time.Parse("2006-01-02", now)
	timeFor := t.Add(-time.Hour * 8)
	parkinfos, err := models.GetAllPark("")
	if err != nil {
		logger.Error("app", fmt.Sprint("计算车场识别率，获取车场列表出错:", err))
		return
	}
	for _, v := range parkinfos {
		countHas, err := models.GetParkRecordCount(v, timeFor.Unix(), "vpl_number != '' ")
		if err != nil {
			logger.Error("app", fmt.Sprint("查询车场进出记录未识别总数出错:", err))
			continue
		}
		counthas, _ := strconv.ParseInt(countHas, 10, 64)
		countAll, err := models.GetParkRecordCount(v, timeFor.Unix(), "")
		if err != nil {
			logger.Error("app", fmt.Sprint("查询车场进出记录总数出错:", err))
			continue
		}
		count, _ := strconv.ParseInt(countAll, 10, 64)
		lostCount := count - counthas
		var result int
		result = 1
		logger.Info("app", fmt.Sprint("count:", count, " lostCount:", lostCount))
		if count == 0 {
			continue
		}
		result, _ = strconv.Atoi(fmt.Sprintf("%d", lostCount/count*100))
		logger.Info("app", fmt.Sprint("计算结果是:", lostCount/count*100))
		parkinfo := models.TParkInfo{RecongnizeRate: result}
		models.UpdateParkinfo(&parkinfo, v.Id)
		time.Sleep(time.Second * 3)
	}
	<-tick.C
}
